<?php


namespace app\staffapi\middleware;


use app\Request;
use app\services\car\StaffAuthServices;
use crmeb\interfaces\MiddlewareInterface;
use think\exception\DbException;
use think\facade\Config;

/**
 * Class AuthTokenMiddleware
 * @package app\api\middleware
 */
class StaffAuthTokenMiddleware implements MiddlewareInterface
{
    public function handle(Request $request, \Closure $next)
    {
        $managerInfo = null;
        $token = trim(ltrim($request->header('Authori-zation'), 'Bearer'));
        if (!$token) $token = trim(ltrim($request->header('Authorization'), 'Bearer'));//正式版，删除此行，某些服务器无法获取到token调整为 Authori-zation
        $services = app()->make(StaffAuthServices::class);
        $managerInfo = $services->parseToken($token);
        Request::macro('managerId', function () use (&$managerInfo) {
            return (int)$managerInfo['id'];
        });

        Request::macro('managerInfo', function () use (&$managerInfo) {
            return $managerInfo;
        });

        return $next($request);
    }
}
